package com.alex.methodreference;

import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.NoArgsConstructor;

import java.util.ArrayList;
import java.util.List;
import java.util.function.Supplier;

/**
 * 员工类
 */
@Data
@NoArgsConstructor
@AllArgsConstructor
class Employee {
    private String name;
    private int age;
    private double salary;

    // 静态方法：检查是否是高薪员工
    public static boolean isHighPaid(Employee e) {
        return e.getSalary() > 10000;
    }

    // 实例方法：获取员工描述
    public String getDescription() {
        return name + " (" + age + "岁)";
    }

}

/**
 * 方法引用示例类
 */
public class Alex008MethodReferenceDemo {
    public static void main(String[] args) {
        // 准备测试数据
        List<Employee> employees = new ArrayList<>();
        employees.add(new Employee("张三", 25, 8000));
        employees.add(new Employee("李四", 30, 12000));
        employees.add(new Employee("王五", 35, 15000));

        // 1. 静态方法引用 (类名::静态方法名)
        System.out.println("===== 1. 静态方法引用 =====");
        // 传统写法
        employees.stream().filter(e -> Employee.isHighPaid(e))
                .forEach(System.out::println);
        // 方法引用写法
        employees.stream().filter(Employee::isHighPaid)
                .forEach(System.out::println);

        // 2. 实例方法引用 (对象::实例方法名)
        System.out.println("\n===== 2. 实例方法引用 =====");
        Employee hr = new Employee("HR", 40, 20000);
        // 使用Supplier函数式接口
        Supplier<String> descSupplier = hr::getDescription;
        System.out.println("HR描述: " + descSupplier.get());

        // 3. 特定类型的实例方法引用 (类名::实例方法名)
        System.out.println("\n===== 3. 特定类型实例方法引用 =====");
        // 传统写法
        employees.stream().map(e -> e.getDescription())
                .forEach(System.out::println);
        // 方法引用写法
        employees.stream().map(Employee::getDescription)
                .forEach(System.out::println);

        // 4. 构造方法引用 (类名::new)
        System.out.println("\n===== 4. 构造方法引用 =====");
        // 无参构造
        Supplier<Employee> employeeSupplier = Employee::new;
        Employee newEmployee = employeeSupplier.get();
        System.out.println("无参构造: " + newEmployee);

        // 三参数构造
        EmployeeFactory factory = Employee::new;
        Employee created = factory.create("赵六", 28, 9000);
        System.out.println("三参构造: " + created);
    }
}

/**
 * 自定义函数式接口(用于演示构造方法引用)
 */
@FunctionalInterface
interface EmployeeFactory {
    Employee create(String name, int age, double salary);
}